<?php
declare(strict_types=1);

namespace App\Controller\WsWorker;

use App\Component\RedisGroupSever;
use App\Component\RedisUserSever;
use App\Component\Server;
use Hyperf\Utils\Codec\Json;


class Talk
{
    public static function talkMsg($fd, $data)
    {
        $room    = $data["room"];
        $content = $data["content"];
        $redisGroup = new RedisGroupSever();
        $roomData = $redisGroup->getRoom($room);

        $redisUser = new RedisUserSever();
        $id = $redisUser->getUserByFd($fd);
        if (!$id) {
            Server::Close($fd);
            return "";
        }
        $user  = json::decode($redisUser->getUserInfo($id));

        $msg = [
            "type"        => "talkMsg",
            "room"        => $room,
            "num"         => count($roomData),
            "client_name" => $user["name"],
            "client_img"  => $user["img"],
            "content"     => htmlspecialchars($content)
        ];

        Server::sendToAll(Json::encode($msg), $roomData);
    }
}